<template>
  <div>
    <h1>vue版计数器</h1>
    <h3>{{count}}</h3>
    <h3>{{evenOrOdd}}</h3>
    <button @click="add">+</button>
    <button @click="decrement">-</button>
    <button @click="addIfEven">如果是偶数+1</button>
    <button @click="decrementIfOdd">如果是奇数延迟1000ms-1</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0
    };
  },
  computed: {
    evenOrOdd() {
      //   this.count % 2
      /*  if (this.count % 2 == 0) {
        return "偶数";
      } else {
        return "奇数";
      } */
      return this.count % 2 == 0 ? "偶数" : "奇数";
    }
  },
  methods: {
    add() {
      //   this.count = this.count + 1;
      this.count++;
    },
    decrement() {
      this.count--;
    },
    addIfEven() {
      if (this.count % 2 == 0) {
        this.count++;
      }
    },
    decrementIfOdd() {
      setTimeout(() => {
        if (this.count % 2 == 1) {
          this.count--;
        }
      }, 1000);
    }
  }
};
</script>

<style>
</style>